import { DeviceCommandModel } from '../../ControlPageAbility/common/models/deviceCommandModel';
import { AddScheduleModel } from '../../ScheduleAbility/common/models/addScheduleModel';
import LogUtil from '../utils/logUtil';
/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License,Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// @ts-ignore
import { axios } from '@ohos/axios'
import http from '@ohos.net.http';
import Url from '@ohos.url'

// 基础路径设置
axios.defaults.baseURL = 'http://139.9.102.131:8080/distschedule-api' //设置baseURL

const TAG = 'DistScheduleService'

// 请求拦截器
axios.interceptors.request.use(
    function (config) {
        // 拦截处理
        LogUtil.info(TAG, 'request interceptors:' + Object.keys(config));
        config.headers.token = "";
        return config;
    },
    function (error) {
        return Promise.reject(error);
    }
);

// 响应拦截器
axios.interceptors.response.use(
    function (response) {
        // 响应处理
        LogUtil.info(TAG, 'response interceptors:' + Object.keys(response));
        response.data.responseInterceptors = '响应拦截器添加的数据'
        return response;
    },
    function (error) {
        return Promise.reject(error);
    }
);


export enum RequestMethod {
    OPTIONS = "OPTIONS",
    GET = "GET",
    HEAD = "HEAD",
    POST = "POST",
    PUT = "PUT",
    DELETE = "DELETE",
    TRACE = "TRACE",
    CONNECT = "CONNECT"
}

/**
 * 数字管家服务端调用类
 */
export class DistScheduleService {
    // static distScheduleServer: string = "http://81.71.17.188:8080/distschedule-api"
    static distScheduleServer: string = "http://139.9.102.131:8080/distschedule-api"
    static readTimeout: number = 3000
    static connectTimeout: number = 3000

    /**
     * 根据手机号，获取用户信息
     * @param phone 手机号
     */
    static async getUserInfo(phone: string) {
        return axios({
            method: "get",
            url: `/user/phone/${phone}`,
            headers: {
                'phone': `${phone}`
            }
        })
    }

    /**
     * 根据用户id，获取设备列表
     * @param phone 手机号
     */
    static async getDeviceListByUserId(userId: string) {
        return axios({
            method: "get",
            url: `/device/userId/${userId}`,
            headers: {
                'userId': `${userId}`
            }
        })
    }

    /**
     * 获取设备类型
     */
    static async getDeviceTypes() {
        return axios({
            method: "get",
            url: `devicetype`,
            headers: {
                'Content-Type': 'application/json'
            }
        })
    }

    /**
     * 根据设备ID获取设备信息
     */
    static async getDeviceInfo(deviceId: string, userId: string) {
        return axios({
            method: "get",
            url: `/device/${deviceId}`,
            headers: {
                'userId': `${userId}`
            }
        })
    }

    /**
     * 根据品类ID获取品类名字作为默认名字
     * @param productId 品类ID
     */
    static async getNameByProId(productId: string) {

        return axios({
            method: "get",
            url: `/devicetype/productId/${productId}`,
            headers: {
                'Content-Type': 'application/json'
            }
        })
    }

    /**
     * 创建设备
     * @param deviceId 设备ID，name 设备名称，ownerId 所有者ID
     */
    static async createDevice(deviceId: string, name: string, ownerId: string, secret: string, typeId: number) {

        return axios({
            method: "post",
            url: `/device`,
            data: { // post传参
                id: deviceId,
                name: name,
                ownerId: ownerId,
                secret: secret,
                typeId: typeId
            }
        })
    }

    /**
     * 根据设备ID获取设备信息
     */
    static async deleteDevice(deviceId: string, userId: string) {

        return axios({
            method: "delete",
            url: `/device/${deviceId}`,
            headers: {
                'userId': `${userId}`
            }
        })
    }

    /**
     * 创建日程
     *
     */
    static async addSchedule(postData: AddScheduleModel) {
        LogUtil.info(TAG, "sendDeviceCommand:" + JSON.stringify(postData))
        return axios({
            method: "post",
            url: `/schedule`,
            // post传参
            data: {
                creatorId: postData.creatorId,
                deviceConfig: postData.deviceConfig,
                endTime: postData.endTime,
                name: postData.name,
                remindDay: postData.remindDay,
                sceneId: postData.sceneId,
                startTime: postData.startTime,
                stateDetail: postData.stateDetail,
                userIds: postData.userIds,
            },
            headers: {
                'Content-Type': 'application/json'
            }
        })
    }

    /**
     * 根据日程ID删除日程
     */
    static async deleteSchedule(scheduleId: string, userId: string) {

        return axios({
            method: "delete",
            url: `/schedule/${scheduleId}`,
            headers: {
                'userId': `${userId}`
            }
        })
    }


    /**
     * 根据时间获取当前的日程数据
     *
     */
    static async getSchedule(userId: string, startTime: string, endTime: string) {

        return axios({
            method: "get",
            url: `/schedule/query?startTime=${startTime}&endTime=${endTime}`,
            headers: {
                'userId': `${userId}`
            }
        })
    }


    /**
     * 按名称搜索日程
     *
     */
    static async searchSchedule(userId: string, strSearch: string) {

        return axios({
            method: "get",
            url: `/schedule/search?name=${strSearch}`,
            headers: {
                'userId': `${userId}`
            }
        })
    }


    /**
     * 根据用户id，查询家庭信息
     *
     */
    static async getFamilyById(userId: string) {

        return axios({
            method: "get",
            url: `/family/userId/${userId}`,
            headers: {
                'Content-Type': 'application/json'
            }
        })
    }


    /**
     * 根据familyId，查询家庭成员信息
     *
     */
    static async getMemberByFamilyId(familyId: string) {

        return axios({
            method: "get",
            url: `/family/${familyId}/member`,
            headers: {
                'Content-Type': 'application/json'
            }
        })
    }
    /**
     * 发送设备控制命令
     * @param phone 手机号
     */
    static async sendDeviceCommand(deviceId: string, postData: DeviceCommandModel) {

        LogUtil.info(TAG, "sendDeviceCommand:" + JSON.stringify(postData))
        return axios({
            method: "post",
            url: `/device/${deviceId}/sendCommand`,
            // post传参
            data: {
                commandName: postData.commandName,
                serviceId: postData.serviceId,
                value: postData.value,
            },
            headers: {
                'Content-Type': 'application/json'
            }
        })
    }
}